package com.study.javaOther.fingerguessing.participation;

import java.io.Serializable;

public class Judgement implements Serializable {
    private int totalNum = 1;
    private Player player1;
    private Player player2;
    private Player winner = null;
    private int num = 0;

    public Player getWinner() {
        return winner;
    }

    public Judgement(Player player1, Player player2) {
        this.player1 = player1;
        this.player2 = player2;
    }

    public void setTotalNum(int num) {
        this.totalNum = num;
    }

    public String showRole() {
        return "\n石头剪刀布规则公告:"
                + "\n    总局数为:" + totalNum + ",若需要请返回更改"
                + "\n    在玩家玩的过程中,若有人先拔得头筹,将提前终止游戏"
                + "\n    例如:5局已经有人胜3局";
    }

    /**
     * @return 1 1赢 2 2赢 0平局 null有人出错了
     */
    public void judgeGame() {
        int a = player1.play();
        int b = player2.play();
        switch (b - a) {
            case 0:
                player1.tieGame();
                player2.tieGame();
                break;
            case 1:
                player1.winGame();
                player2.loseGame();
                break;
            case 2:
                player2.winGame();
                player1.loseGame();
                break;
            case -1:
                player2.winGame();
                player1.loseGame();
                break;
            case -2:
                player1.winGame();
                player2.loseGame();
                break;
            default:
                break;
        }
        System.out.println(player1.getName() + "出的:" + role(a));
        System.out.println(player2.getName() + "出的:" + role(b));
        num++;
    }

    public String role(int type) {
        switch (type) {
            case 0:
                return "石头";
            case 1:
                return "剪刀";
            case 2:
                return "布";
            default:
                return "出错了,不符合规则,请重新记入";
        }
    }

    public boolean showWinner() {
        if (player1.getWin() > (totalNum / 2.0)) {
            this.winner = player1;
            return true;
        }
        if (player2.getWin() > (totalNum / 2.0)) {
            this.winner = player2;
            return true;
        }
        if (num >= totalNum) {
            if (player1.getScore() > player2.getScore()) {
                this.winner = player1;
            } else if (player1.getScore() < player2.getScore()) {
                this.winner = player2;
            }
            return true;
        }
        return false;
    }
}
